package org.rsbot.script.randoms;

import java.util.logging.Logger;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.GroundItems;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Tiles;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.util.Filter;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSGroundItem;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSModel;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPath;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Pwnaz0r", "Taha", "zqqou", "Zach"}, name="FreakyForester", version=2.5D)
public class FreakyForester extends Random
  implements MessageListener
{
  private RSNPC forester;
  private static final int FORESTER_ID = 2458;
  private static final int SEARCH_INTERFACE_ID = 242;
  private static final int PORTAL_ID = 15645;
  private static final RSTile WALK_TO_TILE = new RSTile(2610, 4775);
  private boolean unequip;
  short[] phe;
  Filter<RSNPC> pheasantFilter;
  boolean done;

  public FreakyForester()
  {
    this.unequip = false;
    this.phe = new short[0];
    this.pheasantFilter = new Filter()
    {
      public boolean accept(RSNPC paramRSNPC) {
        Filter localFilter = RSModel.newVertexFilter(FreakyForester.this.phe);
        return localFilter.accept(paramRSNPC.getModel());
      }
    };
    this.done = false;
  }

  public boolean activateCondition()
  {
    if (!this.game.isLoggedIn()) {
      return false;
    }
    this.forester = this.npcs.getNearest(new int[] { 2458 });
    if (this.forester != null) {
      sleep(random(2000, 3000));
      if (this.npcs.getNearest(new int[] { 2458 }) != null) {
        RSObject localRSObject = this.objects.getNearest(new int[] { 15645 });
        return localRSObject != null;
      }
    }
    return false;
  }

  public int getState() {
    if (this.done)
      return 3;
    if (this.interfaces.canContinue())
      return 1;
    if (this.phe.length == 0)
      return 0;
    if (this.inventory.contains(6178))
      return 0;
    if (this.phe.length > 0) {
      return 2;
    }
    return 0;
  }

  public int loop()
  {
    this.forester = this.npcs.getNearest(new int[] { 2458 });
    if (this.forester == null) {
      return -1;
    }
    if (getMyPlayer().getAnimation() != -1)
      return random(3000, 5000);
    if (getMyPlayer().isMoving()) {
      return random(200, 500);
    }
    if (!this.done) {
      this.done = ((searchText(241, "Thank you")) || (this.interfaces.getComponent(242, 4).containsText("leave")));
    }

    if (this.inventory.contains(6179)) {
      this.phe = new short[0];
      this.inventory.getItem(new int[] { 6179 }).doAction("Drop");
      return random(500, 900);
    }
    if ((this.unequip) && (this.inventory.getCount(false) != 28)) {
      if (this.game.getCurrentTab() != 5) {
        this.game.openTab(5);
        sleep(random(1000, 1500));
        this.interfaces.get(387).getComponent(17).doClick();
        return random(1000, 1500);
      }
      return random(100, 500);
    }
    Object localObject;
    if (!this.bank.isDepositOpen()) { if (this.inventory.getCount(false) == 28) if (this.inventory.containsAll(new int[] { 6178 }));  } else {
      if ((this.bank.isDepositOpen()) && (this.bank.getBoxCount() == 28)) {
        this.interfaces.get(11).getComponent(17).getComponent(random(21, 27)).doAction("Deposit");
        return random(1000, 1500);
      }if (this.bank.isDepositOpen()) {
        this.bank.close();
        return random(1000, 1500);
      }
      localObject = this.objects.getNearest(new int[] { 32931 });
      if (((!this.calc.tileOnScreen(((RSObject)localObject).getLocation())) && (this.calc.distanceTo(this.walking.getDestination()) < 8)) || (this.calc.distanceTo(this.walking.getDestination()) > 40)) {
        if (!this.walking.walkTileMM(((RSObject)localObject).getLocation().randomize(3, 3))) {
          this.walking.getPath(((RSObject)localObject).getLocation().randomize(3, 3)).traverse();
        }
        sleep(random(1200, 1400));
      }
      if (((RSObject)localObject).doAction("Deposit")) {
        return random(800, 1200);
      }
    }

    switch (getState()) {
    case 0:
      if ((this.calc.tileOnScreen(this.forester.getLocation())) && (this.calc.distanceTo(this.forester.getLocation()) <= 5)) {
        this.forester.doAction("Talk");
      } else if (this.calc.distanceTo(this.forester.getLocation()) >= 5) {
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(this.forester.getLocation().randomize(3, 3)));
        this.camera.turnToTile(this.forester.getLocation().randomize(3, 3));
      }
      return random(500, 800);
    case 1:
      if (searchText(242, " one"))
        this.phe = Models.oneTail;
      else if (searchText(242, " two"))
        this.phe = Models.twoTail;
      else if (searchText(242, " three")) {
        this.phe = Models.threeTail;
      }
      if (searchText(242, " four")) {
        this.phe = Models.fourTail;
      }
      if (this.interfaces.clickContinue())
        return random(500, 800);
      return random(200, 500);
    case 2:
      if (this.phe.length == 0)
        return random(200, 500);
      localObject = this.npcs.getNearest(this.pheasantFilter);
      RSGroundItem localRSGroundItem = this.groundItems.getNearest(new int[] { 6178 });
      if (localRSGroundItem != null) {
        this.tiles.doAction(localRSGroundItem.getLocation(), "Take");
        return random(600, 900);
      }if (localObject != null)
      {
        if ((this.calc.tileOnScreen(((RSNPC)localObject).getLocation())) && (this.calc.distanceTo(((RSNPC)localObject).getLocation()) <= 5)) {
          ((RSNPC)localObject).doAction("Attack");
          return random(1000, 1500);
        }if (this.calc.distanceTo(((RSNPC)localObject).getLocation()) < 5) break;
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(((RSNPC)localObject).getLocation().randomize(3, 3)));
        this.camera.turnToTile(((RSNPC)localObject).getLocation().randomize(3, 3));
      }
      else
      {
        return random(2000, 5000);
      }
    case 3:
      if (!this.calc.tileOnScreen(WALK_TO_TILE)) {
        if (this.calc.tileOnMap(WALK_TO_TILE))
          this.walking.walkTileMM(WALK_TO_TILE);
        else {
          this.walking.getPath(this.forester.getLocation().randomize(5, 5)).traverse();
        }
        return random(900, 1200);
      }

      RSObject localRSObject = this.objects.getNearest(new int[] { 15645 });

      if (localRSObject == null) {
        this.log.info("Could not find portal.");
        return random(800, 1200);
      }

      if (localRSObject.doAction("Enter"))
        return random(800, 1200);
      return random(200, 500);
    }
    return random(1000, 1500);
  }

  public boolean searchText(int paramInt, String paramString) {
    RSInterface localRSInterface = this.interfaces.get(paramInt);
    if (!localRSInterface.isValid())
      return false;
    for (int i = 0; i < localRSInterface.getChildCount(); i++) {
      if (localRSInterface.getComponent(i).containsText(paramString)) {
        return true;
      }
    }
    return false;
  }

  public void messageReceived(MessageEvent paramMessageEvent) {
    String str = paramMessageEvent.getMessage();
    if (str.contains("no ammo left"))
      this.unequip = true;
  }

  static class Models
  {
    static final short[] oneTail = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 34, 23, 23, 24, 25, 25, 25, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29, 30, 30, 31, 31, 32, 32, 32, 32, 33, 33, 60, 60, 60, 63, 63, 65, 65, 65, 60, 64, 64, 66, 66, 66, 66, 66, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 74, 74, 74, 74, 74, 74, 74, 73, 73, 61, 59, 72, 69, 69, 69, 69, 57, 57, 50, 50, 50, 56, 57, 77, 79, 82, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 44, 95, 95, 95, 95, 95, 95, 85, 85, 86, 86, 86, 87, 87, 101, 101, 101, 101, 101, 101, 102, 102, 102, 102, 103, 103, 103, 104, 104, 104, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106, 107, 107, 108, 108, 108, 94, 35, 35, 36, 36, 36, 37, 37, 38, 110, 111, 111, 111, 111, 111, 111, 112, 113, 99, 99, 99, 100, 100, 100, 131, 97, 98, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 131, 126, 126, 116, 146, 118, 109, 120, 90, 136, 136, 143, 148, 139, 139, 149, 149, 149, 153, 154, 154, 156, 157, 157, 150, 151, 151, 151, 152, 152, 152, 155, 158, 159, 159, 159, 160, 160, 161, 161, 161, 164, 164, 164, 164, 165, 165, 167, 167, 164, 164, 164, 164, 172, 172, 172, 172, 172, 172, 171, 171 };
    static final short[] twoTail = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 34, 23, 23, 24, 25, 25, 25, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29, 30, 30, 31, 31, 32, 32, 32, 32, 33, 33, 60, 60, 60, 63, 63, 65, 65, 65, 60, 64, 64, 66, 66, 66, 66, 66, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 74, 74, 74, 74, 74, 74, 74, 73, 73, 61, 59, 72, 69, 69, 69, 69, 57, 57, 50, 50, 50, 56, 57, 77, 79, 82, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 44, 95, 95, 95, 95, 95, 95, 85, 85, 86, 86, 86, 87, 87, 101, 101, 101, 101, 101, 101, 102, 102, 102, 102, 103, 103, 103, 104, 104, 104, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106, 107, 107, 108, 108, 108, 94, 35, 35, 36, 36, 36, 37, 37, 38, 110, 111, 111, 111, 111, 111, 111, 112, 113, 99, 99, 99, 100, 100, 100, 131, 97, 98, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 131, 126, 126, 116, 146, 118, 109, 120, 90, 136, 136, 143, 148, 139, 139, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150, 151, 151, 151, 151, 152, 152, 152, 152, 153, 153, 153, 154, 154, 154, 154, 155, 155, 166, 166, 156, 165, 155, 155, 156, 156, 169, 169, 172, 172, 172, 172, 172, 172, 161, 162, 162, 162, 163, 163, 163, 164, 158, 158, 158, 175, 175, 175, 175, 176, 176, 176, 176, 184, 184, 185, 185, 178, 182, 182, 182, 182, 177, 189, 189, 189, 189, 190, 190, 188, 188, 179, 179, 179, 160 };
    static final short[] threeTail = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 34, 23, 23, 24, 25, 25, 25, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29, 30, 30, 31, 31, 32, 32, 32, 32, 33, 33, 60, 60, 60, 63, 63, 65, 65, 65, 60, 64, 64, 66, 66, 66, 66, 66, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 74, 74, 74, 74, 74, 74, 74, 73, 73, 61, 59, 72, 69, 69, 69, 69, 57, 57, 50, 50, 50, 56, 57, 77, 79, 82, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 44, 95, 95, 95, 95, 95, 95, 85, 85, 86, 86, 86, 87, 87, 101, 101, 101, 101, 101, 101, 102, 102, 102, 102, 103, 103, 103, 104, 104, 104, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106, 107, 107, 108, 108, 108, 94, 35, 35, 36, 36, 36, 37, 37, 38, 110, 111, 111, 111, 111, 111, 111, 112, 113, 99, 99, 99, 100, 100, 100, 131, 97, 98, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 131, 126, 126, 116, 146, 118, 109, 120, 90, 136, 136, 143, 148, 139, 139, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150, 151, 151, 151, 151, 152, 152, 152, 152, 152, 153, 153, 153, 154, 154, 154, 154, 155, 155, 167, 167, 156, 166, 155, 155, 156, 156, 170, 170, 173, 173, 173, 173, 173, 173, 176, 176, 176, 176, 176, 176, 163, 163, 164, 164, 164, 180, 180, 180, 180, 181, 181, 183, 183, 181, 178, 180, 180, 180, 180, 188, 188, 188, 188, 188, 188, 187, 187, 192, 192, 192, 158, 158, 158, 158, 159, 159, 159, 160, 160, 160, 161, 200, 200, 200, 200, 199, 201, 201, 201, 201, 203, 203, 203, 203, 197, 197, 197, 197, 202, 202, 206, 206, 209, 209 };
    static final short[] fourTail = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 34, 23, 23, 24, 25, 25, 25, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29, 30, 30, 31, 31, 32, 32, 32, 32, 33, 33, 60, 60, 60, 63, 63, 65, 65, 65, 60, 64, 64, 66, 66, 66, 66, 66, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 74, 74, 74, 74, 74, 74, 74, 73, 73, 61, 59, 72, 69, 69, 69, 69, 57, 57, 50, 50, 50, 56, 57, 77, 79, 82, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 44, 95, 95, 95, 95, 95, 95, 85, 85, 86, 86, 86, 87, 87, 101, 101, 101, 101, 101, 101, 102, 102, 102, 102, 103, 103, 103, 104, 104, 104, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106, 107, 107, 108, 108, 108, 94, 35, 35, 36, 36, 36, 37, 37, 38, 110, 111, 111, 111, 111, 111, 111, 112, 113, 99, 99, 99, 100, 100, 100, 131, 97, 98, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 131, 126, 126, 116, 146, 118, 109, 120, 90, 136, 136, 143, 148, 139, 139, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150, 150, 150, 150, 151, 151, 152, 152, 164, 164, 166, 167, 167, 169, 169, 153, 154, 154, 154, 155, 155, 155, 155, 156, 156, 156, 156, 177, 177, 177, 177, 163, 165, 165, 168, 168, 170, 171, 171, 171, 171, 171, 181, 181, 181, 181, 172, 172, 173, 173, 184, 184, 173, 180, 180, 180, 180, 180, 180, 180, 180, 161, 161, 162, 162, 160, 190, 190, 190, 190, 190, 190, 190, 190, 195, 195, 195, 195, 158, 158, 157, 176, 176, 176, 176, 157, 157, 198, 198, 201, 201, 182, 182, 183, 183, 204, 204, 205, 205, 206, 206, 189, 189, 189, 189, 208, 208, 210, 210, 208, 208, 192, 192, 211, 211, 213, 213, 211, 211, 214, 214, 215, 215, 160, 160, 160, 160, 193, 193, 218, 218, 219, 219, 157, 157 };
  }
}